<?php


abstract class BaseStorage extends BaseObject  implements Persistent {


	
	protected static $peer;


	
	protected $id;


	
	protected $user_id;


	
	protected $owner_id;


	
	protected $status_id = 1;


	
	protected $real_name;


	
	protected $mime_type;


	
	protected $uname;


	
	protected $ext;


	
	protected $md5;


	
	protected $file_size;


	
	protected $data_json;


	
	protected $obj_id;


	
	protected $obj_type;


	
	protected $permissions;


	
	protected $updated_at;


	
	protected $created_at;

	
	protected $collStorageI18ns;

	
	protected $lastStorageI18nCriteria = null;

	
	protected $alreadyInSave = false;

	
	protected $alreadyInValidation = false;

  
  protected $culture;

	
	public function getId()
	{

		return $this->id;
	}

	
	public function getUserId()
	{

		return $this->user_id;
	}

	
	public function getOwnerId()
	{

		return $this->owner_id;
	}

	
	public function getStatusId()
	{

		return $this->status_id;
	}

	
	public function getRealName()
	{

		return $this->real_name;
	}

	
	public function getMimeType()
	{

		return $this->mime_type;
	}

	
	public function getUname()
	{

		return $this->uname;
	}

	
	public function getExt()
	{

		return $this->ext;
	}

	
	public function getMd5()
	{

		return $this->md5;
	}

	
	public function getFileSize()
	{

		return $this->file_size;
	}

	
	public function getDataJson()
	{

		return $this->data_json;
	}

	
	public function getObjId()
	{

		return $this->obj_id;
	}

	
	public function getObjType()
	{

		return $this->obj_type;
	}

	
	public function getPermissions()
	{

		return $this->permissions;
	}

	
	public function getUpdatedAt($format = 'Y-m-d H:i:s')
	{

		if ($this->updated_at === null || $this->updated_at === '') {
			return null;
		} elseif (!is_int($this->updated_at)) {
						$ts = strtotime($this->updated_at);
			if ($ts === -1 || $ts === false) { 				throw new PropelException("Unable to parse value of [updated_at] as date/time value: " . var_export($this->updated_at, true));
			}
		} else {
			$ts = $this->updated_at;
		}
		if ($format === null) {
			return $ts;
		} elseif (strpos($format, '%') !== false) {
			return strftime($format, $ts);
		} else {
			return date($format, $ts);
		}
	}

	
	public function getCreatedAt($format = 'Y-m-d H:i:s')
	{

		if ($this->created_at === null || $this->created_at === '') {
			return null;
		} elseif (!is_int($this->created_at)) {
						$ts = strtotime($this->created_at);
			if ($ts === -1 || $ts === false) { 				throw new PropelException("Unable to parse value of [created_at] as date/time value: " . var_export($this->created_at, true));
			}
		} else {
			$ts = $this->created_at;
		}
		if ($format === null) {
			return $ts;
		} elseif (strpos($format, '%') !== false) {
			return strftime($format, $ts);
		} else {
			return date($format, $ts);
		}
	}

	
	public function setId($v)
	{

						if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->id !== $v) {
			$this->id = $v;
			$this->modifiedColumns[] = StoragePeer::ID;
		}

	} 
	
	public function setUserId($v)
	{

						if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->user_id !== $v) {
			$this->user_id = $v;
			$this->modifiedColumns[] = StoragePeer::USER_ID;
		}

	} 
	
	public function setOwnerId($v)
	{

						if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->owner_id !== $v) {
			$this->owner_id = $v;
			$this->modifiedColumns[] = StoragePeer::OWNER_ID;
		}

	} 
	
	public function setStatusId($v)
	{

						if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->status_id !== $v || $v === 1) {
			$this->status_id = $v;
			$this->modifiedColumns[] = StoragePeer::STATUS_ID;
		}

	} 
	
	public function setRealName($v)
	{

						if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->real_name !== $v) {
			$this->real_name = $v;
			$this->modifiedColumns[] = StoragePeer::REAL_NAME;
		}

	} 
	
	public function setMimeType($v)
	{

						if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->mime_type !== $v) {
			$this->mime_type = $v;
			$this->modifiedColumns[] = StoragePeer::MIME_TYPE;
		}

	} 
	
	public function setUname($v)
	{

						if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->uname !== $v) {
			$this->uname = $v;
			$this->modifiedColumns[] = StoragePeer::UNAME;
		}

	} 
	
	public function setExt($v)
	{

						if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->ext !== $v) {
			$this->ext = $v;
			$this->modifiedColumns[] = StoragePeer::EXT;
		}

	} 
	
	public function setMd5($v)
	{

						if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->md5 !== $v) {
			$this->md5 = $v;
			$this->modifiedColumns[] = StoragePeer::MD5;
		}

	} 
	
	public function setFileSize($v)
	{

						if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->file_size !== $v) {
			$this->file_size = $v;
			$this->modifiedColumns[] = StoragePeer::FILE_SIZE;
		}

	} 
	
	public function setDataJson($v)
	{

						if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->data_json !== $v) {
			$this->data_json = $v;
			$this->modifiedColumns[] = StoragePeer::DATA_JSON;
		}

	} 
	
	public function setObjId($v)
	{

						if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->obj_id !== $v) {
			$this->obj_id = $v;
			$this->modifiedColumns[] = StoragePeer::OBJ_ID;
		}

	} 
	
	public function setObjType($v)
	{

						if ($v !== null && !is_int($v) && is_numeric($v)) {
			$v = (int) $v;
		}

		if ($this->obj_type !== $v) {
			$this->obj_type = $v;
			$this->modifiedColumns[] = StoragePeer::OBJ_TYPE;
		}

	} 
	
	public function setPermissions($v)
	{

						if ($v !== null && !is_string($v)) {
			$v = (string) $v; 
		}

		if ($this->permissions !== $v) {
			$this->permissions = $v;
			$this->modifiedColumns[] = StoragePeer::PERMISSIONS;
		}

	} 
	
	public function setUpdatedAt($v)
	{

		if ($v !== null && !is_int($v)) {
			$ts = strtotime($v);
			if ($ts === -1 || $ts === false) { 				throw new PropelException("Unable to parse date/time value for [updated_at] from input: " . var_export($v, true));
			}
		} else {
			$ts = $v;
		}
		if ($this->updated_at !== $ts) {
			$this->updated_at = $ts;
			$this->modifiedColumns[] = StoragePeer::UPDATED_AT;
		}

	} 
	
	public function setCreatedAt($v)
	{

		if ($v !== null && !is_int($v)) {
			$ts = strtotime($v);
			if ($ts === -1 || $ts === false) { 				throw new PropelException("Unable to parse date/time value for [created_at] from input: " . var_export($v, true));
			}
		} else {
			$ts = $v;
		}
		if ($this->created_at !== $ts) {
			$this->created_at = $ts;
			$this->modifiedColumns[] = StoragePeer::CREATED_AT;
		}

	} 
	
	public function hydrate(ResultSet $rs, $startcol = 1)
	{
		try {

			$this->id = $rs->getInt($startcol + 0);

			$this->user_id = $rs->getInt($startcol + 1);

			$this->owner_id = $rs->getInt($startcol + 2);

			$this->status_id = $rs->getInt($startcol + 3);

			$this->real_name = $rs->getString($startcol + 4);

			$this->mime_type = $rs->getString($startcol + 5);

			$this->uname = $rs->getString($startcol + 6);

			$this->ext = $rs->getString($startcol + 7);

			$this->md5 = $rs->getString($startcol + 8);

			$this->file_size = $rs->getInt($startcol + 9);

			$this->data_json = $rs->getString($startcol + 10);

			$this->obj_id = $rs->getInt($startcol + 11);

			$this->obj_type = $rs->getInt($startcol + 12);

			$this->permissions = $rs->getString($startcol + 13);

			$this->updated_at = $rs->getTimestamp($startcol + 14, null);

			$this->created_at = $rs->getTimestamp($startcol + 15, null);

			$this->resetModified();

			$this->setNew(false);

						return $startcol + 16; 
		} catch (Exception $e) {
			throw new PropelException("Error populating Storage object", $e);
		}
	}

	
	public function delete($con = null)
	{
		if ($this->isDeleted()) {
			throw new PropelException("This object has already been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(StoragePeer::DATABASE_NAME);
		}

		try {
			$con->begin();
			StoragePeer::doDelete($this, $con);
			$this->setDeleted(true);
			$con->commit();
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

	
	public function save($con = null)
	{
    if ($this->isModified() && !$this->isColumnModified(StoragePeer::UPDATED_AT))
    {
      $this->setUpdatedAt(time());
    }

    if ($this->isNew() && !$this->isColumnModified(StoragePeer::CREATED_AT))
    {
      $this->setCreatedAt(time());
    }

		if ($this->isDeleted()) {
			throw new PropelException("You cannot save an object that has been deleted.");
		}

		if ($con === null) {
			$con = Propel::getConnection(StoragePeer::DATABASE_NAME);
		}

		try {
			$con->begin();
			$affectedRows = $this->doSave($con);
			$con->commit();
			return $affectedRows;
		} catch (PropelException $e) {
			$con->rollback();
			throw $e;
		}
	}

	
	protected function doSave($con)
	{
		$affectedRows = 0; 		if (!$this->alreadyInSave) {
			$this->alreadyInSave = true;


						if ($this->isModified()) {
				if ($this->isNew()) {
					$pk = StoragePeer::doInsert($this, $con);
					$affectedRows += 1; 										 										 
					$this->setId($pk);  
					$this->setNew(false);
				} else {
					$affectedRows += StoragePeer::doUpdate($this, $con);
				}
				$this->resetModified(); 			}

			if ($this->collStorageI18ns !== null) {
				foreach($this->collStorageI18ns as $referrerFK) {
					if (!$referrerFK->isDeleted()) {
						$affectedRows += $referrerFK->save($con);
					}
				}
			}

			$this->alreadyInSave = false;
		}
		return $affectedRows;
	} 
	
	protected $validationFailures = array();

	
	public function getValidationFailures()
	{
		return $this->validationFailures;
	}

	
	public function validate($columns = null)
	{
		$res = $this->doValidate($columns);
		if ($res === true) {
			$this->validationFailures = array();
			return true;
		} else {
			$this->validationFailures = $res;
			return false;
		}
	}

	
	protected function doValidate($columns = null)
	{
		if (!$this->alreadyInValidation) {
			$this->alreadyInValidation = true;
			$retval = null;

			$failureMap = array();


			if (($retval = StoragePeer::doValidate($this, $columns)) !== true) {
				$failureMap = array_merge($failureMap, $retval);
			}


				if ($this->collStorageI18ns !== null) {
					foreach($this->collStorageI18ns as $referrerFK) {
						if (!$referrerFK->validate($columns)) {
							$failureMap = array_merge($failureMap, $referrerFK->getValidationFailures());
						}
					}
				}


			$this->alreadyInValidation = false;
		}

		return (!empty($failureMap) ? $failureMap : true);
	}

	
	public function getByName($name, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = StoragePeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->getByPosition($pos);
	}

	
	public function getByPosition($pos)
	{
		switch($pos) {
			case 0:
				return $this->getId();
				break;
			case 1:
				return $this->getUserId();
				break;
			case 2:
				return $this->getOwnerId();
				break;
			case 3:
				return $this->getStatusId();
				break;
			case 4:
				return $this->getRealName();
				break;
			case 5:
				return $this->getMimeType();
				break;
			case 6:
				return $this->getUname();
				break;
			case 7:
				return $this->getExt();
				break;
			case 8:
				return $this->getMd5();
				break;
			case 9:
				return $this->getFileSize();
				break;
			case 10:
				return $this->getDataJson();
				break;
			case 11:
				return $this->getObjId();
				break;
			case 12:
				return $this->getObjType();
				break;
			case 13:
				return $this->getPermissions();
				break;
			case 14:
				return $this->getUpdatedAt();
				break;
			case 15:
				return $this->getCreatedAt();
				break;
			default:
				return null;
				break;
		} 	}

	
	public function toArray($keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = StoragePeer::getFieldNames($keyType);
		$result = array(
			$keys[0] => $this->getId(),
			$keys[1] => $this->getUserId(),
			$keys[2] => $this->getOwnerId(),
			$keys[3] => $this->getStatusId(),
			$keys[4] => $this->getRealName(),
			$keys[5] => $this->getMimeType(),
			$keys[6] => $this->getUname(),
			$keys[7] => $this->getExt(),
			$keys[8] => $this->getMd5(),
			$keys[9] => $this->getFileSize(),
			$keys[10] => $this->getDataJson(),
			$keys[11] => $this->getObjId(),
			$keys[12] => $this->getObjType(),
			$keys[13] => $this->getPermissions(),
			$keys[14] => $this->getUpdatedAt(),
			$keys[15] => $this->getCreatedAt(),
		);
		return $result;
	}

	
	public function setByName($name, $value, $type = BasePeer::TYPE_PHPNAME)
	{
		$pos = StoragePeer::translateFieldName($name, $type, BasePeer::TYPE_NUM);
		return $this->setByPosition($pos, $value);
	}

	
	public function setByPosition($pos, $value)
	{
		switch($pos) {
			case 0:
				$this->setId($value);
				break;
			case 1:
				$this->setUserId($value);
				break;
			case 2:
				$this->setOwnerId($value);
				break;
			case 3:
				$this->setStatusId($value);
				break;
			case 4:
				$this->setRealName($value);
				break;
			case 5:
				$this->setMimeType($value);
				break;
			case 6:
				$this->setUname($value);
				break;
			case 7:
				$this->setExt($value);
				break;
			case 8:
				$this->setMd5($value);
				break;
			case 9:
				$this->setFileSize($value);
				break;
			case 10:
				$this->setDataJson($value);
				break;
			case 11:
				$this->setObjId($value);
				break;
			case 12:
				$this->setObjType($value);
				break;
			case 13:
				$this->setPermissions($value);
				break;
			case 14:
				$this->setUpdatedAt($value);
				break;
			case 15:
				$this->setCreatedAt($value);
				break;
		} 	}

	
	public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
	{
		$keys = StoragePeer::getFieldNames($keyType);

		if (array_key_exists($keys[0], $arr)) $this->setId($arr[$keys[0]]);
		if (array_key_exists($keys[1], $arr)) $this->setUserId($arr[$keys[1]]);
		if (array_key_exists($keys[2], $arr)) $this->setOwnerId($arr[$keys[2]]);
		if (array_key_exists($keys[3], $arr)) $this->setStatusId($arr[$keys[3]]);
		if (array_key_exists($keys[4], $arr)) $this->setRealName($arr[$keys[4]]);
		if (array_key_exists($keys[5], $arr)) $this->setMimeType($arr[$keys[5]]);
		if (array_key_exists($keys[6], $arr)) $this->setUname($arr[$keys[6]]);
		if (array_key_exists($keys[7], $arr)) $this->setExt($arr[$keys[7]]);
		if (array_key_exists($keys[8], $arr)) $this->setMd5($arr[$keys[8]]);
		if (array_key_exists($keys[9], $arr)) $this->setFileSize($arr[$keys[9]]);
		if (array_key_exists($keys[10], $arr)) $this->setDataJson($arr[$keys[10]]);
		if (array_key_exists($keys[11], $arr)) $this->setObjId($arr[$keys[11]]);
		if (array_key_exists($keys[12], $arr)) $this->setObjType($arr[$keys[12]]);
		if (array_key_exists($keys[13], $arr)) $this->setPermissions($arr[$keys[13]]);
		if (array_key_exists($keys[14], $arr)) $this->setUpdatedAt($arr[$keys[14]]);
		if (array_key_exists($keys[15], $arr)) $this->setCreatedAt($arr[$keys[15]]);
	}

	
	public function buildCriteria()
	{
		$criteria = new Criteria(StoragePeer::DATABASE_NAME);

		if ($this->isColumnModified(StoragePeer::ID)) $criteria->add(StoragePeer::ID, $this->id);
		if ($this->isColumnModified(StoragePeer::USER_ID)) $criteria->add(StoragePeer::USER_ID, $this->user_id);
		if ($this->isColumnModified(StoragePeer::OWNER_ID)) $criteria->add(StoragePeer::OWNER_ID, $this->owner_id);
		if ($this->isColumnModified(StoragePeer::STATUS_ID)) $criteria->add(StoragePeer::STATUS_ID, $this->status_id);
		if ($this->isColumnModified(StoragePeer::REAL_NAME)) $criteria->add(StoragePeer::REAL_NAME, $this->real_name);
		if ($this->isColumnModified(StoragePeer::MIME_TYPE)) $criteria->add(StoragePeer::MIME_TYPE, $this->mime_type);
		if ($this->isColumnModified(StoragePeer::UNAME)) $criteria->add(StoragePeer::UNAME, $this->uname);
		if ($this->isColumnModified(StoragePeer::EXT)) $criteria->add(StoragePeer::EXT, $this->ext);
		if ($this->isColumnModified(StoragePeer::MD5)) $criteria->add(StoragePeer::MD5, $this->md5);
		if ($this->isColumnModified(StoragePeer::FILE_SIZE)) $criteria->add(StoragePeer::FILE_SIZE, $this->file_size);
		if ($this->isColumnModified(StoragePeer::DATA_JSON)) $criteria->add(StoragePeer::DATA_JSON, $this->data_json);
		if ($this->isColumnModified(StoragePeer::OBJ_ID)) $criteria->add(StoragePeer::OBJ_ID, $this->obj_id);
		if ($this->isColumnModified(StoragePeer::OBJ_TYPE)) $criteria->add(StoragePeer::OBJ_TYPE, $this->obj_type);
		if ($this->isColumnModified(StoragePeer::PERMISSIONS)) $criteria->add(StoragePeer::PERMISSIONS, $this->permissions);
		if ($this->isColumnModified(StoragePeer::UPDATED_AT)) $criteria->add(StoragePeer::UPDATED_AT, $this->updated_at);
		if ($this->isColumnModified(StoragePeer::CREATED_AT)) $criteria->add(StoragePeer::CREATED_AT, $this->created_at);

		return $criteria;
	}

	
	public function buildPkeyCriteria()
	{
		$criteria = new Criteria(StoragePeer::DATABASE_NAME);

		$criteria->add(StoragePeer::ID, $this->id);

		return $criteria;
	}

	
	public function getPrimaryKey()
	{
		return $this->getId();
	}

	
	public function setPrimaryKey($key)
	{
		$this->setId($key);
	}

	
	public function copyInto($copyObj, $deepCopy = false)
	{

		$copyObj->setUserId($this->user_id);

		$copyObj->setOwnerId($this->owner_id);

		$copyObj->setStatusId($this->status_id);

		$copyObj->setRealName($this->real_name);

		$copyObj->setMimeType($this->mime_type);

		$copyObj->setUname($this->uname);

		$copyObj->setExt($this->ext);

		$copyObj->setMd5($this->md5);

		$copyObj->setFileSize($this->file_size);

		$copyObj->setDataJson($this->data_json);

		$copyObj->setObjId($this->obj_id);

		$copyObj->setObjType($this->obj_type);

		$copyObj->setPermissions($this->permissions);

		$copyObj->setUpdatedAt($this->updated_at);

		$copyObj->setCreatedAt($this->created_at);


		if ($deepCopy) {
									$copyObj->setNew(false);

			foreach($this->getStorageI18ns() as $relObj) {
				$copyObj->addStorageI18n($relObj->copy($deepCopy));
			}

		} 

		$copyObj->setNew(true);

		$copyObj->setId(NULL); 
	}

	
	public function copy($deepCopy = false)
	{
				$clazz = get_class($this);
		$copyObj = new $clazz();
		$this->copyInto($copyObj, $deepCopy);
		return $copyObj;
	}

	
	public function getPeer()
	{
		if (self::$peer === null) {
			self::$peer = new StoragePeer();
		}
		return self::$peer;
	}

	
	public function initStorageI18ns()
	{
		if ($this->collStorageI18ns === null) {
			$this->collStorageI18ns = array();
		}
	}

	
	public function getStorageI18ns($criteria = null, $con = null)
	{
				include_once 'lib/model/om/BaseStorageI18nPeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		if ($this->collStorageI18ns === null) {
			if ($this->isNew()) {
			   $this->collStorageI18ns = array();
			} else {

				$criteria->add(StorageI18nPeer::ID, $this->getId());

				StorageI18nPeer::addSelectColumns($criteria);
				$this->collStorageI18ns = StorageI18nPeer::doSelect($criteria, $con);
			}
		} else {
						if (!$this->isNew()) {
												

				$criteria->add(StorageI18nPeer::ID, $this->getId());

				StorageI18nPeer::addSelectColumns($criteria);
				if (!isset($this->lastStorageI18nCriteria) || !$this->lastStorageI18nCriteria->equals($criteria)) {
					$this->collStorageI18ns = StorageI18nPeer::doSelect($criteria, $con);
				}
			}
		}
		$this->lastStorageI18nCriteria = $criteria;
		return $this->collStorageI18ns;
	}

	
	public function countStorageI18ns($criteria = null, $distinct = false, $con = null)
	{
				include_once 'lib/model/om/BaseStorageI18nPeer.php';
		if ($criteria === null) {
			$criteria = new Criteria();
		}
		elseif ($criteria instanceof Criteria)
		{
			$criteria = clone $criteria;
		}

		$criteria->add(StorageI18nPeer::ID, $this->getId());

		return StorageI18nPeer::doCount($criteria, $distinct, $con);
	}

	
	public function addStorageI18n(StorageI18n $l)
	{
		$this->collStorageI18ns[] = $l;
		$l->setStorage($this);
	}

  public function getCulture()
  {
    return $this->culture;
  }

  public function setCulture($culture)
  {
    $this->culture = $culture;
  }

  public function getName()
  {
    $obj = $this->getCurrentStorageI18n();

    return ($obj ? $obj->getName() : null);
  }

  public function setName($value)
  {
    $this->getCurrentStorageI18n()->setName($value);
  }

  public function getText()
  {
    $obj = $this->getCurrentStorageI18n();

    return ($obj ? $obj->getText() : null);
  }

  public function setText($value)
  {
    $this->getCurrentStorageI18n()->setText($value);
  }

  protected $current_i18n = array();

  public function getCurrentStorageI18n()
  {
    if (!isset($this->current_i18n[$this->culture]))
    {
      $obj = StorageI18nPeer::retrieveByPK($this->getId(), $this->culture);
      if ($obj)
      {
        $this->setStorageI18nForCulture($obj, $this->culture);
      }
      else
      {
        $this->setStorageI18nForCulture(new StorageI18n(), $this->culture);
        $this->current_i18n[$this->culture]->setCulture($this->culture);
      }
    }

    return $this->current_i18n[$this->culture];
  }

  public function setStorageI18nForCulture($object, $culture)
  {
    $this->current_i18n[$culture] = $object;
    $this->addStorageI18n($object);
  }

} 